package com.yhgm.controller;

import com.yhgm.constant.Constant;
import com.yhgm.mapper.SqlBackupsDao;
import com.yhgm.utils.FileUtils;
import com.yhgm.utils.R;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("yw")
public class YWController {
    @Resource
    private SqlBackupsDao sqlBackupsDao;
    @Resource
    private FileUtils fileUtils;

    @GetMapping("getUsableSpace")
    //@SaCheckRole(Constant.SUPER_ADMIN)
    public R getUsableSpace() {
        Map<String, Object> data = new HashMap<>();
        File diskPartition = new File("/");
        long usableSpace = diskPartition.getUsableSpace();
        long totalSpace = diskPartition.getTotalSpace();
        data.put("usableSpace", usableSpace / Constant.B_TO_G);
        data.put("totalSpace", totalSpace / Constant.B_TO_G);
        return R.ok(data, "");
    }

    @GetMapping("getSqlFileList")
    //@SaCheckRole(Constant.SUPER_ADMIN)
    public R getSqlFileList(Integer page, Integer size) {
        if (page < 1) {
            return R.error("page couldn't less than 1");
        } else {
            page = (page - 1) * size;
            Map<String, Object> data = new HashMap<>();
            data.put("list", sqlBackupsDao.getList(page, size));
            data.put("total", sqlBackupsDao.count());
            return R.ok(data, "getSqlFileList");
        }
    }

    @GetMapping("downloadSqlFile")
    //@SaCheckRole(Constant.SUPER_ADMIN)
    public void downloadSqlFile(String path, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        //http://8.136.8.93:39001/yw/downloadSqlFile?path=sql/backups_2023/07/05.sql
        File file = new File(Constant.proBiologyImagePath + path);
        long length = file.length();
        response.addHeader("Content-Length", String.valueOf(length));
//        response.addHeader("Content-Disposition",
//                "attachment;filename=" + URLEncoder.encode(path, "utf-8"));
        //表示以二进制流的形式返回
        //response.setContentType("text/x-sql");
        //设置Content-Disposition为inline
        response.addHeader("Content-Disposition", "attachment;filename="
                + URLEncoder.encode(path.substring(path.indexOf("/")+1), "utf-8").replaceAll("\\+", "%20"));
        //下面这行代码是解决浏览器预览文件时中文乱码的
        MediaType mediaType = MediaTypeFactory.getMediaType("a.sql").get();
        response.setContentType(mediaType.getType() + ";charset=UTF-8");
        response.addHeader("Content-Length", String.valueOf(length));
        fileUtils.out(file,response);
    }

    @DeleteMapping("deleteSqlFile")
    //@SaCheckRole(Constant.SUPER_ADMIN)
    public R deleteSqlFile(Long id){
        String path=Constant.proBiologyImagePath+Constant.SQL_BACKUPS_PATH+sqlBackupsDao.getFilenameById(id);
        File file=new File(path);
        if (file.exists()) {
            file.delete();
        }
        sqlBackupsDao.deleteOneById(id);
        return R.ok("deleteSqlFile");
    }
}
